package com.yaoweigroup.community.adapter;

import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.ShopType;
import com.yaoweigroup.community.enity.Shops;
import com.yaoweigroup.community.util.BitmapHelp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShopAdapter extends BaseAdapter {

	private Context context;
	private List<ShopType> list;
	private LayoutInflater mInflater;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	public ShopAdapter(Context context, List<ShopType> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;

		bitmapUtils = BitmapHelp.getBitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.adapter_shop_item, null);
			convertView.setTag(holder);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.des_tv = (TextView) convertView.findViewById(R.id.des_tv);
		} else {
			holder = (Holder) convertView.getTag();
		}

		ShopType type = list.get(position);
		holder.name_tv.setText(type.getTypename());
		holder.des_tv.setText(type.getTypename());
		if (isVisibleImg) {
			holder.icon_iv.setVisibility(View.VISIBLE);
			bitmapUtils.display(holder.icon_iv, type.getProductTypeImgUrl(),
					bigPicDisplayConfig);
		} else {
			holder.icon_iv.setVisibility(View.GONE);

		}

		return convertView;
	}

	public final class Holder {

		ImageView icon_iv;
		TextView name_tv;
		TextView des_tv;

	}

	private boolean isVisibleImg = true;

	public void setIsVisibleImg(boolean isVisible) {
		this.isVisibleImg = isVisible;
		notifyDataSetChanged();
	}

	public void setDataChange(List<ShopType> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
